﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess_Layer;
using Business_Layer;
using System.Collections;

public partial class MyAdmin_ChiTietLichSu_AM : System.Web.UI.Page
{
    LogAction log = new LogAction();
    protected void Page_Load(object sender, EventArgs e)
    {
   
        if (Helper.getCurrentAdminQuyen(this.Master) == QuyenTruyCap.QuyenXem)
        {
            // xử lý quyền xem
            ((CommandField)gvDSChiTietLichSu_AM.Columns[0]).ShowDeleteButton = false;
            CommandField dtCmdField = ((CommandField)dtvChiTietLichSu.Fields[dtvChiTietLichSu.Fields.Count - 1]);
            dtCmdField.ShowEditButton = false;
            dtCmdField.ShowInsertButton = false;
            XoaNhieuBUTTON.Visible = false;
        }
        else if (Helper.getCurrentAdminQuyen(this.Master) == QuyenTruyCap.QuyenChinhSua)
        {
            // xử lý quyền chỉnh sửa
        }

        List<int> checkedCheckBoxRowIndex = getCheckedCheckBoxRowIndexes(gvDSChiTietLichSu_AM);
        initDataSource();
        restoreCheckedCheckBox(checkedCheckBoxRowIndex);

        lblThongBaoDTView.Visible = false;
        lblThongBaoGridView.Visible = false;

    }

    private List<int> getCheckedCheckBoxRowIndexes(GridView gvDSChiTietLichSu_AM)
    {
        if (gvDSChiTietLichSu_AM.Rows.Count == 0)
        {
            return null;
        }
        
        List<int> rowIndexes = new List<int>();
        int lastCellIndex = gvDSChiTietLichSu_AM.Rows[0].Cells.Count - 1;
        foreach (GridViewRow r in gvDSChiTietLichSu_AM.Rows)
        {
            CheckBox checkXoa = (CheckBox)r.Cells[lastCellIndex].FindControl("checkXoa");
            if (checkXoa.Checked)
            {
                rowIndexes.Add(r.RowIndex);
            }
        }

        return rowIndexes;
    }

    private void restoreCheckedCheckBox(List<int> checkedCheckBoxRowIndex)
    {
        if (checkedCheckBoxRowIndex == null)
        {
            return;
        }

        int lastCellIndex = gvDSChiTietLichSu_AM.Rows[0].Cells.Count - 1;
        foreach (int rowIndex in checkedCheckBoxRowIndex)
        {
            ((CheckBox)gvDSChiTietLichSu_AM.Rows[rowIndex].Cells[lastCellIndex].FindControl("checkXoa")).Checked = true;
        }
    }

    protected void XoaNhieuBUTTON_Click(object sender, EventArgs e)
    {
        string ids = "";
        List<long> deletedIDs = new List<long>();
        int lastCellIndex = gvDSChiTietLichSu_AM.Rows[0].Cells.Count - 1;
        foreach (GridViewRow r in gvDSChiTietLichSu_AM.Rows)
        {
            CheckBox checkXoa = (CheckBox)r.Cells[lastCellIndex].FindControl("checkXoa");
            if (checkXoa.Checked)
            {
                long id = long.Parse(r.Cells[1].Text);
                deletedIDs.Add(id);
                ids += id.ToString() + ", ";
            }
        }

        lblThongBaoGridView.Visible = true;
        lblThongBaoGridView.Text = "Xóa thành công record: " + ids.Substring(0, ids.LastIndexOf(", "));
        //log
        log.TenLoaiCapNhat = "Xóa";
        log.Ngay = DateTime.Now;
        log.TenBang = "Bảng Chi Tiết Lịch Sử";
        log.TenLoaiDangNhap = "Admin";
        log.TenDangNhap = Helper.getCurrentAdminUserName(Session);
        string logText = "";
        log.DuLieuCu = "record ID:" + ids.Substring(0, ids.LastIndexOf(", "));
        logText = "x";
        log.DuLieuMoi = logText;
        Log_BUS.insert(log);
        //log
        try
        {
            DataAccess_Layer.ChiTietLichSu_DAC.delete(deletedIDs);

        }
        catch (System.Exception ex)
        {
            lblThongBaoGridView.Text = "Xóa thất bại!";
        }

        initDataSource();
        refreshDetailViewPageIndex();
    }

    private void refreshGV()
    {
        gvDSChiTietLichSu_AM.DataSource = DataAccess_Layer.ChiTietLichSu_DAC.layHet();
        gvDSChiTietLichSu_AM.DataBind();
    }

    private void initDataSource()
    {
        refreshGV();
        gvDSChiTietLichSu_AM.SelectedIndex = gvDSChiTietLichSu_AM.SelectedIndex == -1 ? 0 : gvDSChiTietLichSu_AM.SelectedIndex;
        dtvChiTietLichSu.PageIndex = Helper.calPageIndexForDetailView(gvDSChiTietLichSu_AM);
    }

    private void refreshDetailViewPageIndex()
    {
        dtvChiTietLichSu.DataBind();
        dtvChiTietLichSu.PageIndex = Helper.calPageIndexForDetailView(gvDSChiTietLichSu_AM);
    }

    protected void gvDSChiTietLichSu_AM_PageIndexChanged(object sender, EventArgs e)
    {
        refreshDetailViewPageIndex();
    }


    protected void gvDSChiTietLichSu_AM_SelectedIndexChanged(object sender, EventArgs e)
    {
        refreshDetailViewPageIndex();
    }


    protected void gvDSChiTietLichSu_AM_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        Helper.changeGVPageIndex(gvDSChiTietLichSu_AM, e.NewPageIndex);
        gvDSChiTietLichSu_AM.SelectedIndex = gvDSChiTietLichSu_AM.Rows.Count > 0 ? 0 : -1;
        dtvChiTietLichSu.PageIndex = Helper.calPageIndexForDetailView(gvDSChiTietLichSu_AM);
    }
    protected void dtvChiTietLichSu_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
        refreshGV();
        Helper.changeGVPageIndex(gvDSChiTietLichSu_AM, gvDSChiTietLichSu_AM.PageCount - 1);
        gvDSChiTietLichSu_AM.SelectedIndex = gvDSChiTietLichSu_AM.Rows.Count - 1;
        refreshDetailViewPageIndex();
    }


    protected void dtvChiTietLichSu_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
    {
        refreshGV();
        refreshDetailViewPageIndex();
    }


    protected void LQDSChiTietLichSu_Inserted(object sender, LinqDataSourceStatusEventArgs e)
    {//log
        try
        {
            log.TenLoaiCapNhat = "Thêm";
            log.Ngay = DateTime.Now;
            log.TenBang = "Bảng Chi tiết lịch sử";
            log.TenLoaiDangNhap = "Adminstator";
            log.TenDangNhap = Helper.getCurrentAdminUserName(Session);

            DataAccess_Layer.ChiTietLichSu logO;

            log.DuLieuCu = "X";

            logO = (DataAccess_Layer.ChiTietLichSu)e.Result;
            log.DuLieuMoi = "ID Lịch sử: " + logO.IDLichSu
                + "..\nTên Sản phẩm: " + logO.TenSanpham
                + "..\nGiá Tiền: " + logO.GiaTien
                + "..\nGiá tiền: " + logO.GiaTien
                + "..\nThành tiền: " + logO.ThanhTien;
            Log_BUS.insert(log);
        }
        catch { }
        //log
        lblThongBaoDTView.Visible = true;
        DataAccess_Layer.ChiTietLichSu newOrder = (DataAccess_Layer.ChiTietLichSu)e.Result;

        if (e.Exception == null)
        {
            lblThongBaoDTView.Text = "Thêm thành công record có ID là " + newOrder.ID;
            return;
        }

        lblThongBaoDTView.Text = "Thêm thất bại record!";
        e.ExceptionHandled = true;
    }
    protected void LQDSChiTietLichSu_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        e.Result = gvDSChiTietLichSu_AM.DataSource;
    }
    protected void LQDSChiTietLichSu_Updating(object sender, LinqDataSourceUpdateEventArgs e)
    {
        //log
        try
        {
            log.TenLoaiCapNhat = "cập nhật";
            log.Ngay = DateTime.Now;
            log.TenBang = "Bảng Chi tiết lịch sử";
            log.TenLoaiDangNhap = "Adminstator";
            log.TenDangNhap = Helper.getCurrentAdminUserName(Session);
        
            DataAccess_Layer.ChiTietLichSu logO=(DataAccess_Layer.ChiTietLichSu)e.OriginalObject;

            log.DuLieuCu = "ID Lịch sử: " + logO.IDLichSu
                + "..\nTên Sản phẩm: " + logO.TenSanpham
                + "..\nGiá Tiền: " + logO.GiaTien
                + "..\nGiá tiền: " + logO.GiaTien
                + "..\nThành tiền: " + logO.ThanhTien;

            logO = (DataAccess_Layer.ChiTietLichSu)e.NewObject;
            log.DuLieuMoi = "ID Lịch sử: " + logO.IDLichSu
                + "..\nTên Sản phẩm: " + logO.TenSanpham
                + "..\nGiá Tiền: " + logO.GiaTien
                + "..\nGiá tiền: " + logO.GiaTien
                + "..\nThành tiền: " + logO.ThanhTien;
            Log_BUS.insert(log);
        }
        catch { }
        //log
        DataAccess_Layer.ChiTietLichSu newOrder = (DataAccess_Layer.ChiTietLichSu)e.NewObject;
        lblThongBaoDTView.Visible = true;
        try
        {
            DataAccess_Layer.ChiTietLichSu_DAC.update(newOrder);
            lblThongBaoDTView.Text = "Cập nhật thành công record có ID là " + newOrder.ID;
        }
        catch (System.Exception ex)
        {
            lblThongBaoDTView.Text = "Cập nhật thất bại record có ID là " + newOrder.ID;
        }
        e.Cancel = true;
    }
    protected void gvDSChiTietLichSu_AM_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
       
    }
    protected void dtvChiTietLichSu_ModeChanging(object sender, DetailsViewModeEventArgs e)
    {
        
    }
    protected void gvDSChiTietLichSu_AM_RowCommand(object sender, GridViewCommandEventArgs e)
    {


    }
}